package com.panny.newcar.util;

import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Stack;

import com.mysql.jdbc.PreparedStatement;

/**
 * @ClassName: ConnectionPool
 * @Description: TODO 数据库连接池，项目中很重要的使用类。使用方法---Connection
 *               conn=ConnectionPool.getConnection("autohome","carvoice.tpddns.cn");
 *               因要求不同调用不同的方法使用
 * @author: Alexis
 * @date: 2016年7月22日 下午12:46:39
 */
public class ConnectionPool {

	Stack<Connection> st = new Stack<Connection>();
	private static ConnectionPool instance = new ConnectionPool();
	public static int initialPoolSize = 10;
	public static String db_Name = "qczj001";
	public static String userName = "root";
	public static String userPasswd = "123456";
	public static String host = "localhost";
	public static String port = "3306";

	/**
	 * @Title: getConnection
	 * @Description: TODO
	 *               获得Connection，参数为数据库名称（autohome），端口IP地址（carvoice.tpddns.cn),推荐使用这个方法，用户名和密码198和168的数据库都是一样的
	 * @param db
	 * @param hostip
	 * @return
	 * @return: Connection
	 * @author: Alexis
	 */
	public synchronized static Connection getConnection(String dbName, String IP) {
		db_Name = dbName;
		host = IP;
		if (instance.st.size() > 0)
			return instance.st.pop();
		else
			return createConnection();
	}

	public synchronized static Connection getConnection(String dbName, String IP, String ports) {
		db_Name = dbName;
		host = IP;
		port = ports;
		if (instance.st.size() > 0)
			return instance.st.pop();
		else
			return createConnection();
	}

	public synchronized static Connection getConnection(String db, String user, String pwd, String hostip) {
		db_Name = db;
		userName = user;
		userPasswd = pwd;
		host = hostip;
		if (instance.st.size() > 0)
			return instance.st.pop();
		else
			return createConnection();
	}

	/**
	 * @Title: getConnection
	 * @Description: TODO 获得默认的Connection，数据库配置参数为类中的静态属性
	 * @return
	 * @return: Connection
	 * @author: Alexis
	 */
	public synchronized static Connection getConnection() {
		if (instance.st.size() > 0)
			return instance.st.pop();
		else
			return createConnection();
	}

	synchronized static void returnConnection(Connection conn) {
		instance.st.push(conn);
	}

	/**
	 * @Title: createConnection
	 * @Description: TODO
	 *               创建Connection连接，连接池的默认数量为10个连接，当所用的连接数大于默认值时，创建新的数据库操作对象Connection
	 * @return
	 * @return: Connection
	 * @author: Alexis
	 */
	private static Connection createConnection() {
		String url = "jdbc:mysql://" + host + ":" + port + "/" + db_Name + "?user=" + userName + "&password="
				+ userPasswd + "&useCursorFetch=true&defaultFetchSize=50000&characterEncoding=UTF-8";
		Connection connection;
		Connection proxy = null;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(url);
			ConnectionProxy handler = new ConnectionProxy(connection);
			proxy = (Connection) Proxy.newProxyInstance(connection.getClass().getClassLoader(),
					new Class[] { Connection.class }, handler);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	/**
	 * @Title: close
	 * @Description: TODO 关闭数据库操作，参数不同关闭的不同，注意关闭的先后顺序
	 * @param rs
	 * @param st
	 * @param conn
	 * @return: void
	 * @author: Alexis
	 */
	public static void close(ResultSet rs, Statement st, Connection conn) {
		try {

			rs.close();
			st.close();
			if (conn != null) {
				conn.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Statement st, Connection conn) {
		try {

			st.close();
			if (conn != null) {
				conn.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(PreparedStatement ps, Connection conn) {
		try {

			ps.close();
			if (conn != null) {
				conn.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
